package net.sahv.bdyz.controller.admin;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import net.sahv.bdyz.model.Admin;
import net.sahv.bdyz.service.AdminRoleService;
import net.sahv.bdyz.service.AdminService;
import net.sahv.bdyz.util.PagingUtil;
import net.sahv.bdyz.util.Security;
import net.sahv.bdyz.util.VerifyCode;
/**
 * admin表
 * @author Administrator
 *
 */
@Controller
@RequestMapping("admin/admin")
public class AdminController {

	@Autowired
	private AdminService adminService;
	@Autowired
	private AdminRoleService adminRoleService;

	/**
	 * 获取验证码
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "/verifyCode", method = RequestMethod.GET)
	public String getVerifyCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		VerifyCode vc=new VerifyCode();
		vc.getVerifyCode(req, resp);
		return null;
	}

	/**
	 * 跳转到管理员列表
	 * @author 张天敏
	 * @see 2016年7月5日 17:54:25
	 * @param model
	 * @param offset
	 * @return
	 */
	@RequestMapping(value = "/adminList", method = RequestMethod.GET)
	public String adminList(Model model) {
		return "admin/admin/adminList";
	}
	
	/**
	 * 管理员列表
	 * @author 张天敏
	 * @see 2016年7月5日 17:54:43
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/adminList")
	public JSON adminList(PagingUtil pagingUtil,HttpServletRequest request,Model model){
		
		//获取查询参数
		String A_NAME=request.getParameter("A_NAME");
		String A_PHONE=request.getParameter("A_PHONE");
		
		String sortField=" ORDER BY a.`A_CREATE_TIME` DESC ";
		String where="";
		
		where+=" WHERE 1=1 ";
		
		
		
		if(A_NAME!=null&&!A_NAME.trim().equals("")){
			where+=" AND a.`A_NAME` LIKE '%"+A_NAME+"%' ";
		}
		
		if(A_PHONE!=null&&!A_PHONE.trim().equals("")){
			where+=" AND a.`A_PHONE` LIKE '%"+A_PHONE+"%'  ";
		}
		
		Map<String,Object>  param = new HashMap<String,Object>();
		
		param.put("start", pagingUtil.getiDisplayStart());
		param.put("length", pagingUtil.getiDisplayLength());
		param.put("where", where);
		param.put("sortField", sortField);
		
		List<Map<String, Object>> adminList = adminService.adminList(param);
		//统计信息的总条数
		Integer adminListCount=adminService.adminListCount(where);
		
		Map<String,Object>  map = new HashMap<String,Object>();
		map.put("aaData", adminList);
		//查出来总共有多少条记录
		map.put("iTotalRecords", adminListCount);
		map.put("iTotalDisplayRecords", adminListCount);
		map.put("sEcho", pagingUtil.getsEcho());
		 
		return (JSON) JSON.toJSON(map);
	}
	
	/**
	 * 跳转到添加管理员页面
	 * @author 张天敏
	 * @see 2016年7月5日 18:17:52
	 * @return
	 */
	@RequestMapping(value = "/insertAdmin",method=RequestMethod.GET)
	public String insertAdmin() {
		return "admin/admin/insertAdmin";
	}
	
	/**
	 * 添加管理员信息
	 * @author 张天敏
	 * @see 2016年7月5日 18:37:47
	 * @param admin
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insertAdmin",method=RequestMethod.POST)
	public ApiResult insertAdmin(Admin admin){
		ApiResult result=new ApiResult();
		admin.setaCreateTime(new Date());
		admin.setaIsDelete(0);
		Security security=new Security();
		String passwordSecurity=security.encryptAES(admin.getaPassword(),Security.generateAesKey("sahv"));
		admin.setaPassword(passwordSecurity);
		int flag=adminService.insert(admin);
		if(flag==1){
			result.msg.status=0;
			result.msg.desc="添加成功！";
		}else if(flag==0){
			result.msg.status=1;
			result.msg.desc="添加失败！";
		}
		return result;
	}
	
	/**
	 * 跳转到修改管理员信息界面
	 * @author 张天敏
	 * @see 2016年7月5日 18:41:45
	 * @return
	 */
	@RequestMapping(value="/updateAdmin",method=RequestMethod.GET)
	public String updateAdmin(Model model,Integer id){
		Admin admin=adminService.selectByPrimaryKey(id);
		model.addAttribute("admin", admin);
		return "admin/admin/updateAdmin";
	}
	
	/**
	 * 修改管理员信息
	 * @author 张天敏
	 * @see 2016年7月5日 18:40:29
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateAdmin",method=RequestMethod.POST)
	public ApiResult updateAdmin(Admin admin){
		ApiResult result=new ApiResult();
		Admin admin2=adminService.selectByPrimaryKey(admin.getaId());
		admin2.setaImg(admin.getaImg());
		admin2.setaName(admin.getaName());
		//登陆账号不允许修改。
		//admin2.setaUsername(admin.getaUsername());
		admin2.setaPhone(admin.getaPhone());
		admin2.setaSex(admin.getaSex());
		admin2.setaQq(admin.getaQq());
		admin2.setaEmail(admin.getaEmail());
		//如果前台传入的是加密的字符串，说明密码没有被更改，则不做任何操作。
		if(!admin.getaPassword().equals("aaaaaa")&&!admin.getaPassword().equals("")){
			Security security=new Security();
			String passwordSecurity=security.encryptAES(admin.getaPassword(),Security.generateAesKey("sahv"));
			admin2.setaPassword(passwordSecurity);
		}
		int flag=adminService.updateByPrimaryKey(admin2);
		if(flag==1){
			result.msg.status=0;
			result.msg.desc="修改成功！";
		}else if(flag==0){
			result.msg.status=1;
			result.msg.desc="修改失败！";
		}
		return result;
	}
	
	/**
	 * 删除管理员信息
	 * @author 张天敏
	 * @see 2016年7月5日 18:48:56
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteAdmin",method=RequestMethod.POST)
	public ApiResult deleteAdmin(Integer[] id){
		ApiResult result=new ApiResult();
		int flag=0;
		for(Integer i:id){
			//删除后台用户
			flag=adminService.deleteByPrimaryKey(i);
			//同步删除用户角色关联表中的信息
			adminRoleService.deleteByAdminId(i);
			if(flag==0){
				result.msg.status=1;
				result.msg.desc="删除失败！";
				return result;
			}
		}
		if(flag==1){
			result.msg.status=0;
			result.msg.desc="删除成功！";
		}
		return result;
	}

	/**
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/main")
	public String main(Model model) {
		return "admin/main";
	}
	
	/**
	 * 跳转到登录界面
	 * @author 张天敏
	 * @see 2016年7月4日 15:18:11
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(Model model,@ModelAttribute(value="message") String message){
		model.addAttribute("message", message);
		return "admin/login";
	}
	
	/**
	 * 验证登陆账号是否已存在
	 * @author 张天敏
	 * @date 日期：2016年8月9日 时间：上午11:35:36
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="validAUsername",method=RequestMethod.POST)
	public JSON validAUsername(String param){
		String info="";
		String status="";
		Map<String,Object>  map = new HashMap<String,Object>();
		int count=adminService.validAUsername(param);
		if(count>0){
			status="n";
			info="登陆账号已存在！";
		}else{
			status="y";
			info="可以使用";
		}
		map.put("status", status);
		map.put("info", info);
		return (JSON) JSON.toJSON(map);
	}
	

}
